package nickthissen.iracing.activities;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockListActivity;

import nickthissen.iracing.iracingforum2.R;

/**
 * Created by Nick on 22-5-13.
 */
public class SherlockUtils
{
    public static void setNotificationBar(Activity activity)
    {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        if (!prefs.getBoolean(activity.getString(R.string.PREF_GeneralShowNotification), true))
        {
            activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    public static void setMultilineTitle(SherlockActivity context, String titleText)
    {
        ActionBar bar = context.getSupportActionBar();
        bar.setDisplayShowCustomEnabled(true);
        bar.setCustomView(R.layout.actionbar_title);

        TextView txt = (TextView) context.findViewById(R.id.action_bar_title);
        txt.setText(titleText);
        setTitleClickListener(context, txt);

        context.findViewById(R.id.action_bar_progress).setVisibility(View.GONE);
        context.findViewById(R.id.action_bar_refreshbutton).setVisibility(View.GONE);
    }

    public static void setMultilineTitle(SherlockListActivity context, String titleText)
    {
        ActionBar bar = context.getSupportActionBar();
        bar.setDisplayShowCustomEnabled(true);
        bar.setCustomView(R.layout.actionbar_title);

        TextView txt = (TextView) context.findViewById(R.id.action_bar_title);
        txt.setText(titleText);
        setTitleClickListener(context, txt);

        context.findViewById(R.id.action_bar_progress).setVisibility(View.GONE);
        context.findViewById(R.id.action_bar_refreshbutton).setVisibility(View.GONE);
    }

    private static void setTitleClickListener(final Context context, final TextView txt)
    {
        txt.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(context, txt.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });
    }

    public static void setProgressVisibility(SherlockActivity context, boolean visible)
    {
        ProgressBar pb = (ProgressBar) context.findViewById(R.id.action_bar_progress);
        if (visible)
            pb.setVisibility(View.VISIBLE);
        else
            pb.setVisibility(View.GONE);
    }

    public static void setRefreshButtonVisibility(SherlockActivity context, boolean visible)
    {
        ImageButton button = getRefreshButton(context);
        if (visible)
            button.setVisibility(View.VISIBLE);
        else
            button.setVisibility(View.GONE);
    }

    public static ImageButton getRefreshButton(SherlockActivity context)
    {
        return (ImageButton) context.findViewById(R.id.action_bar_refreshbutton);
    }
}
